<template>
  <div class="xminds-card-grid">
    <el-skeleton :loading="skeletonLoading" animated>
      <template #template>
        <div class="card-skeleton-title"><el-skeleton-item /></div>
        <div class="card-skeleton"><el-skeleton-item v-for="item in 5" :key="item" /></div>
        <div class="card-skeleton-title"><el-skeleton-item /></div>
        <div class="card-skeleton"><el-skeleton-item v-for="item in 10" :key="item" /></div>
      </template>
      <template #default>
        <div class="box-modal" v-if="showFolder && folderList.length">
          <div class="title">文件夹</div>
          <div class="list">
            <div class="folder" v-for="item in folderList" :key="item.id" @click="handleFolderClick(item)">
              <div class="logo">
                <img src="@/assets/images/home/folder.png" alt="">
              </div>
              <div class="desc">
                <p class="name">{{ item.name }}</p>
                <p class="tip">{{ item.childFolderCount + item.childMindCount }} 项目</p>
              </div>
              <el-popover
                :width="200"
                :show-arrow="false"
                :teleported="false"
                :hide-after="300"
                :show-after="200"
                placement="bottom-start">
                <ul>
                  <li
                    v-for="itemName in FOLDER_OPERATE_MENUS"
                    :class="{ danger: itemName.command === 'delete' }"
                    :key="itemName.command"
                    @click.stop="handleOperateFolder(itemName.command, item)">
                    <i :class="itemName.icon"></i>
                    <span>{{ itemName.name }}</span>
                  </li>
                </ul>
                <template #reference>
                  <i class="more ri-more-line" @click.stop></i>
                </template>
              </el-popover>
            </div>
          </div>
        </div>
        <div class="box-modal" v-if="showMind && mindList.length">
          <div class="title">
            导图
            <p class="batch-operate" v-show="selectMindIds.length">
                <span>已选择 {{ selectMindIds.length }} 项</span>
                <el-button v-if="type === 'trash'" type="danger" plain size="small" @click="handleDeleteMind(selectMindIds)">删除</el-button>
                <el-button v-if="type === 'work'" type="danger" plain size="small" @click="handleTrashMind(selectMindIds)">移入回收站</el-button>
                <el-button size="small" @click="handleCancelCheckMind">取消</el-button>
              </p>
          </div>
          <div class="list">
            <div class="mind" :class="{ selected: selectMindIds.includes(item.id) }" v-for="item in mindList" :key="item.id" @click="handleMindClick(item)">
              <div class="image-preview">
                <img :src="item.coverImage" alt="">
              </div>
              <div class="desc">
                <p class="name">{{ item.name }}</p>
                <p class="tip">最后修改：{{ item.updateTime }}</p>
                <el-popover
                  :width="200"
                  :show-arrow="false"
                  :teleported="false"
                  :hide-after="300"
                  :show-after="200"
                  placement="bottom-start">
                  <ul>
                    <li
                      v-for="itemName in MIND_OPERATE_MENUS"
                      :class="{ danger: itemName.command === 'delete' }"
                      :key="itemName.command"
                      @click.stop="handleOperateMind(itemName.command, item)">
                      <i :class="itemName.icon"></i>
                      <span>{{ itemName.name }}</span>
                    </li>
                  </ul>
                  <template #reference>
                    <i class="more ri-more-line" @click.stop></i>
                  </template>
                </el-popover>
                <div class="select-mind" :class="{ show: selectMindIds.includes(item.id) }" @click.stop>
                  <el-checkbox v-model="item.selected"></el-checkbox>
                </div>
              </div>
              <div class="collect-tag"
                v-if="type === 'work'"
                :class="{ active: item.isCollected === 1 }"
                @click.stop="updateMindCollectState(item.id, item.isCollected !== 1)">
                <i class="ri-star-fill" v-if="item.isCollected === 1"></i>
                <i class="ri-star-line" v-else></i>
              </div>
            </div>
          </div>
        </div>
        <div class="empty-mind-text" v-if="!folderList.length && !mindList.length">未找到Gmind文件</div>
      </template>
    </el-skeleton>

    <NewFolderMindModal
      v-if="folderRenameModalVisible"
      :folderDetail="folderDetail"
      :mindDetail="mindDetail"
      :operateType="operateType"
      @close-modal="handleCloseModal" />

    <MoveToFolder
      v-if="moveToFolderModalVisible"
      :folderDetail="folderDetail"
      :mindDetail="mindDetail"
      :operateType="operateType"
      @close-modal="handleCloseModal" />

    <ShareMindModal v-if="shareModalVisible" :mindId="mindDetail?.id" @close-modal="handleCloseModal" />
  </div>
</template>

<script setup lang="ts">
import { computed, defineProps, PropType, defineEmits } from 'vue'
import useViewCardList from '@/hooks/useViewCardList'
import type { Folder, Mind } from '@/types'

const props = defineProps({
  skeletonLoading: Boolean,
  showFolder: {
    type: Boolean,
    default: false
  },
  showMind: {
    type: Boolean,
    default: true
  },
  folderList: {
    type: Array as PropType<Folder[]>,
    default: () => []
  },
  mindList: {
    type: Array as PropType<Mind[]>,
    default: () => []
  },
  type: {
    type: String as PropType<'work' | 'trash'>,
    default: 'work'
  }
})

const emits = defineEmits(['refresh-page', 'update-collect-status', 'folder-click', 'mind-click'])

const selectMindIds = computed(() => {
  return props.mindList.filter(m => m.selected).map(m => m.id)
})

const handleCancelCheckMind = () => {
  props.mindList.forEach(m => {
    m.selected = false
  })
}

const {
  FOLDER_OPERATE_MENUS,
  MIND_OPERATE_MENUS,
  folderRenameModalVisible,
  shareModalVisible,
  moveToFolderModalVisible,
  folderDetail,
  mindDetail,
  operateType,
  handleFolderClick,
  handleMindClick,
  handleOperateFolder,
  handleOperateMind,
  handleCloseModal,
  updateMindCollectState,
  handleTrashMind,
  handleDeleteMind,
  MoveToFolder,
  ShareMindModal,
  NewFolderMindModal
} = useViewCardList(emits, props.type)
</script>

<style lang="scss" scoped>
.xminds-card-grid {
  padding: 8px 2px;
  min-height: 360px;

  .card-skeleton-title {
    margin-top: 36px;
    .el-skeleton__item  {
      width: 160px;
      height: 36px;
    }
  }

  .card-skeleton {
    margin-top: 20px;
    gap: 16px;
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    .el-skeleton__item {
      height: auto;
      height: 150px;
    }
  }

  .box-modal {
     & + .box-modal {
      margin-top: 36px;
     }
    .title {
      font-size: 13px;
      line-height: 18px;
      color: #949699;
      display: flex;
      margin-top: 6px;
      height: 36px;
      align-items: center;
      justify-content: space-between;
      .batch-operate {
        display: flex;
        align-items: center;
        color: #333;
        .el-button {
          margin-left: 8px;
        }
      }
    }
    .list {
      display: grid;
      grid-gap: 24px 32px;
      grid-template-columns: repeat(auto-fill, minmax(275px, 1fr));
      margin-top: 12px;
      .folder, .mind {
        border: 1px solid rgba(0, 0, 0, .08);
        border-radius: 8px;
        padding: 12px 24px;
        background-color: #FAFAFB;
        position: relative;
        &.selected {
          border: 2px solid var(--el-color-primary);
        }
        .logo {
          font-size: 0;
          img {
            width: 32px;
          }
        }
        .image-preview {
          font-size: 0;
          border-bottom: 1px solid rgba(0, 0, 0, .08);
          aspect-ratio: 2.05;
          overflow: hidden;
          border-radius: 8px 8px 0 0;
          &:hover {
            img {
              transform: scale(1.3);
            }
          }
          img {
            width: 100%;
            height: 100%;
            -o-object-fit: cover;
            object-fit: cover;
            max-width: 100%;
            display: block;
            transition: 0.3s;
          }
        }
        .more {
          position: absolute;
          right: 12px;
          top: 12px;
          color: #999;
          background-color: rgba(0, 0, 0, .05);
          border-radius: 4px;
          padding: 0 4px;
          opacity: 0;
          transition: 0.2s;
        }
        ul {
          li {
            padding: 8px 6px;
            border-radius: 6px;
            cursor: pointer;
            display: flex;
            align-items: center;
            color: #333;
            &.danger {
              color: #b10a0a;
            }
            & + li {
              margin-top: 4px;
            }
            &:hover {
              background-color: #e1e1e190;
            }
            i {
              font-size: 18px;
              position: relative;
              top: 1px;
            }
            span {
              font-size: 14px;
              margin-left: 6px;
            }
          }
        }
        .collect-tag {
          position: absolute;
          top: 12px;
          right: 12px;
          border: 1px solid rgba(0, 0, 0, .08);
          background-color: #FAFAFB;
          padding: 4px;
          font-size: 0;
          border-radius: 6px;
          cursor: pointer;
          display: none;
          i {
            color: #999;
            font-size: 18px;
          }
          &.active {
            display: block;
            i {
              color: var(--el-color-primary);
            }
          }
        }
        &:hover {
          .more {
            opacity: 1;
          }
          .collect-tag {
            display: block;
          }
          .select-mind {
            opacity: 1;
          }
        }
      }
      .mind {
        padding: 0;
        .desc {
          padding: 8px 14px 12px 14px;
          .more {
            top: 6px;
          }
        }
      }
      .desc {
        margin-top: 8px;
        padding-left: 3px;
        position: relative;
        .name {
          font-size: 12px;
          color: #202020;
          font-weight: 600;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
          padding-right: 30px;
        }
        .tip {
          font-size: 12px;
          color: #999;
          margin-top: 6px;
        }
        .select-mind {
          position: absolute;
          right: 12px;
          bottom: 4px;
          opacity: 0;
          &.show {
            opacity: 1;
          }
        }
      }
    }
  }
  .empty-mind-text {
    text-align: center;
    font-size: 15px;
    color: #999;
    margin-top: 72px;
  }
}
</style>
